//
//  MenuToolAgent.m
//  DigiOps
//
//  Created by app on 2018/9/29.
//  Copyright © 2018年 俞渊华. All rights reserved.
//

#import "MenuToolAgent.h"
@interface MenuToolAgent()

@property(nonatomic, strong)NSMutableArray *aticleArray;
@property(nonatomic, strong)NSMutableArray *selectedArray;
@property(nonatomic, strong)NSMutableArray *allArray;
@end

@implementation MenuToolAgent
singleton_implementation(MenuToolAgent)
-(NSMutableArray *)allArray{
    if (!_allArray) {
        _allArray = [NSMutableArray array];
        AticleMenu * menu1 = [[AticleMenu alloc]init];
        menu1.resident = YES;
        menu1.menuId = -1;
        menu1.selected = YES;
        menu1.isEditing = NO;
        menu1.Text = @"任务";
        

        //        [_allArray addObject:menu1];
        //        [_allArray addObject:menu2];
    }
    return _allArray;
}
+(void)load{
    AticleMenu * menu1 = [[AticleMenu alloc]init];
    menu1.resident = YES;
    menu1.menuId = -1;
    menu1.selected = YES;
    menu1.isEditing = NO;
    menu1.Text = @"任务";

    [menu1 saveOrUpdate];

    
    NSMutableArray * newArray = [NSMutableArray array];
    NSArray * locationArray = [AticleMenu findAll];
    
    //    [FBNetworkTool get:@"articleapi/com.GetAticleMenu" parameters:@{@"iType":@"0"} showHUD:NO success:^(XMHttpResponseModel *responseModel) {
    //        if (responseModel.code == 200) {
    //            NSArray * array = [AticleMenu mj_objectArrayWithKeyValuesArray:responseModel.data];
    //            ///判断用户有没有在本地选择过
    //            if ([XMUserDefaults boolForKey:@"userSelectedMenu"]) {
    //                ///选择过了，拿到的数据跟本地数据对比。有新增的保存本地
    //                for (AticleMenu * menu in array) {
    //                    for (AticleMenu * location in locationArray) {
    //                        if (menu.menuId != location.menuId && ![menu.Text isEqualToString:location.Text]) {
    //                            [newArray addObject:menu];
    //                        }
    //                    }
    //                }
    //
    //                if (newArray.count > 0) {
    //                    [AticleMenu saveArray:newArray];
    //                }
    //            }else{//用户没有编辑过，本地没有保存。
    //                if (array.count > 0) {
    //                    NSInteger number = (array.count >= 6)?6:array.count;
    //                    for (NSInteger i = 0; i < number; i++) {
    //                        AticleMenu * menu = array[i];
    //                        menu.selected = YES;
    //                        [newArray addObject:menu];
    //                    }
    //                    [AticleMenu saveArray:array];
    //                }
    //            }
    //        }
    //    } failure:^(NSError *error) {
    //    }];
}

-(NSArray *)localSelectedMenus{
    NSArray * array = [AticleMenu findWhere:@[@"selected",@"=",@(YES)]];
    [self.allArray addObjectsFromArray:array];
    return self.allArray;
}

+(NSArray *)userSelectedMenu{
    return [AticleMenu findWhere:@[@"selected",@"=",@(YES)]];
}
+(NSArray *)userUnselectedMenu{
    return [AticleMenu findWhere:@[@"selected",@"=",@(NO)]];
}


@end

